//
//  SuccessfulEnrollmentController.m
//  这家测试项目
//
//  Created by IOS_DEVELOPER on 2017/6/5.
//  Copyright © 2017年 陈志凌. All rights reserved.
//

#import "ZJSuccessfulEnrollmentController.h"

@interface ZJSuccessfulEnrollmentController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (weak, nonatomic) IBOutlet UIButton *cancelMoneyBtn;
@property (strong, nonatomic) ZJUserModel * userModel;
@property (weak, nonatomic) IBOutlet UILabel *proName;
@property (weak, nonatomic) IBOutlet UIImageView *headImg;

@property (weak, nonatomic) IBOutlet UILabel *renwuLbale;
@property (weak, nonatomic) IBOutlet UILabel *tuiguangLabel;
@property (weak, nonatomic) IBOutlet UILabel *qiangfangLabel;
@property (strong, nonatomic) IBOutletCollection(UIImageView) NSArray *rightImgs;
@property (strong, nonatomic)UIAlertController *alertController;

/**<数据模型>*/
@property (nonatomic,strong) ZJProjectDetailModel *model;
@end

@implementation ZJSuccessfulEnrollmentController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
    self.model = GET_PROJECTINFO;
    self .userModel = GET_USERINFO;
    
    if ([self.userModel.is_sincere integerValue] != 1) {
        self.userModel.is_sincere = @"1";
        allConstAuthenticated = @"1";
        SYN_USER(self.userModel)
    }
    
    self.tableview.layer.shadowColor = RGB(235, 235, 235).CGColor;;
    self.tableview.layer.shadowOpacity = 0.8f;
    self.tableview.layer.shadowOffset = CGSizeMake(0,0.1);
    
    self.proName.text = [NSString stringWithFormat:@"感谢您报名:%@",self.model.project_name];
}

-(void)initView{
    
    BaseNavigationController * nvc = self.navigationController;
    
    if (self.isBackRoot) {
        nvc.bakcRootView = YES;
    }else{
        nvc.bakcRootView = NO;
    }
    
    if ([self.deposit_return_type isEqualToString:@"已退诚意金"]) {
        
        self.cancelMoneyBtn.hidden = YES;
        
    }else if ([self.deposit_return_type isEqualToString:@"退诚意金"]){
        
        self.cancelMoneyBtn.hidden = NO;
        
    }else{
        self.cancelMoneyBtn.hidden = YES;
    }
    self.headImg.image = [UIImage imageWithContentsOfFile:[Utils imgPath:@"tqbm_pic_cg@3x.png"]];
    [self.cancelMoneyBtn setBackgroundColor:[Utils colorWithHexString:allConstHexColor]];
    self.renwuLbale.textColor = [Utils colorWithHexString:allConstHexColor];
    self.tuiguangLabel.textColor = [Utils colorWithHexString:allConstHexColor];
    self.qiangfangLabel.textColor = [Utils colorWithHexString:allConstHexColor];
    
    for (UIImageView *imgv in self.rightImgs) {
        imgv.image = [UIImage imageWithContentsOfFile:[Utils imgPath:@"tqbm_icon_yjh@3x.png"]];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * cellA = @"cellA";
    static NSString * cellB = @"cellB";
    static NSString * cellC = @"cellC";
    
    UITableViewCell *cell;
    UILabel * label;
    UIImageView * imgv;
    
    switch (indexPath.row) {
        case 0:
            cell = [tableView dequeueReusableCellWithIdentifier:cellA forIndexPath:indexPath];;
       
            imgv = [cell viewWithTag:1004];
            imgv.image = [UIImage imageWithContentsOfFile:[Utils imgPath:@"tqbm_icon_yjh@3x.png"]];
            label = [cell viewWithTag:1003];
            label.textColor = [Utils colorWithHexString:allConstHexColor];
            break;
            
        case 1:

            cell = [tableView dequeueReusableCellWithIdentifier:cellB forIndexPath:indexPath];;

            label = [cell viewWithTag:1001];
            label.text = self.model.project_name;
            break;
            
        case 2:
            cell = [tableView dequeueReusableCellWithIdentifier:cellC forIndexPath:indexPath];
            label = [cell viewWithTag:1002];
            if (self.userModel.deposit.length > 2) {
                label.text = [NSString stringWithFormat:@"%@元",self.userModel.deposit];
            }else{
                label.text = [NSString stringWithFormat:@"%@元",self.model.deposit_money];
            }
            break;
            
        default:
            break;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return  120;
    }else{
        return  60;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
//退款
- (IBAction)cancelMoneyBtn:(UIButton *)sender {
    
    self.alertController = [UIAlertController alertControllerWithTitle:@"注意" message:@"退款后将不再享有在线购房及专享优惠等特权，确认退款吗？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *resetAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [self.alertController dismissViewControllerAnimated:YES completion:nil];
    }];
    
    [self.alertController addAction:resetAction];
    UIAlertAction *resetAction1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        [HUD showLoading:@"提交中..."];
        NSDictionary *parameter = [LRNetwork requestParameter:@{}];
        NSString *url = [LRNetwork requestURL:@"cgi/deposit_return.php"];
        
        [LRNetwork netRequestPOSTWithRequestURL:url withParameter:parameter withReturnValeuBlock:^(id returnValue) {
            
            [[[UIAlertView alloc] initWithTitle:@"退款通知" message:@"退款成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
            
            self.userModel.money_total = [NSString stringWithFormat:@"%.2f",[self.userModel.money_total floatValue] + [returnValue[@"money"] floatValue]];
            self.userModel.is_sincere = @"0";
            SYN_USER(self.userModel)
            [HUD hide];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:kHomeDataChange object:nil];
            [self.navigationController popToRootViewControllerAnimated:YES];
            
        } withErrorCodeBlock:^(NSInteger errorCode, NSString *errorMessage) {
            [HUD showErrorMsg:errorMessage];
        } withFailureBlock:^{
            
        }];
        
    }];
    
    [self.alertController addAction:resetAction1];
    [self presentViewController:_alertController animated:YES completion:nil];

}
#pragma mark - 补全分隔线左侧缺失
- (void)viewDidLayoutSubviews {
    if ([self.tableview respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableview setSeparatorInset:UIEdgeInsetsZero];
        
    }
    if ([self.tableview respondsToSelector:@selector(setLayoutMargins:)])  {
        [self.tableview setLayoutMargins:UIEdgeInsetsZero];
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}
-(void)hide{
    
    [self.alertController dismissViewControllerAnimated:YES completion:nil];
    
}
@end
